﻿/***********************************************************************
 * <copyright file="SqlServerPermissionDao.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Wednesday, March 19, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessSecurity;
using DataObjects;
using DataObjects.ADO;
using DataSecurity.StandartInterfaces;

namespace DataSecurity.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// SqlServerPermission class
    /// </summary>
    public class SqlServerPermissionDao : IPermission
    {
        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public Permission Get(int id)
        {
            const string sql = @"uspPermission_GetByID";
            object[] parms = { "@Id", id };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public Permission Get(string code)
        {
            const string sql = @"uspPermission_GetByCode";
            object[] parms = { "@Code", code };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Kiểm tra quyền trong site.
        /// </summary>
        /// <param name="siteId">The site identifier.</param>
        /// <param name="perCode">The per code.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public Permission CheckExistInRoleInSite(int siteId, string perCode, int userId)
        {
            const string sql = @"uspGet_RoleInSite";
            object[] parms = { "@SitePath", siteId, "@PermissionCode", perCode, "@UserId", userId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <returns></returns>
        public List<Permission> GetList()
        {
            const string sql = @"uspPermission_GetList";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }

        /// <summary>
        /// Gets the list by user and site.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="siteId">The site identifier.</param>
        /// <returns></returns>
        public List<Permission> GetListByUserAndSite(int userId, int siteId)
        {
            const string sql = @"uspPermission_GetByUserAndSite";
            object[] parms = { "@UserId", userId, "@SiteId", siteId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalPage">The total page.</param>
        /// <returns></returns>
        public List<Permission> GetPaged(int currentPage, ref int totalPage)
        {
            const string sql = @"uspPermission_GetPaging";
            object[] parms = { "@CurrentPage", currentPage, "@TotalPage", totalPage };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, out totalPage, parms);
        }

        /// <summary>
        /// Inserts the specified permission.
        /// </summary>
        /// <param name="permission">The permission.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Insert(Permission permission)
        {
            const string sql = @"uspPermission_Insert";
            return Db.Insert(sql, CommandType.StoredProcedure, TakeInsert(permission));
        }

        /// <summary>
        /// Updates the specified permission.
        /// </summary>
        /// <param name="permission">The permission.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Update(Permission permission)
        {
            const string sql = @"uspPermission_Update";
            return Db.Update(sql, CommandType.StoredProcedure, TakeUpdate(permission));
        }

        /// <summary>
        /// Deletes the specified permission.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        public object Delete(string idList)
        {
            const string sql = @"uspPermission_Delete";
            object[] parms = { "@IdList", idList };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }
        
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Permission> Make = reader =>
          new Permission
          {
              Id = reader["Id"].AsInt(),
              Title = reader["Title"].AsString(),
              Code = reader["Code"].AsString(),
              Description = reader["Description"].AsString(),
              Order = reader["Order"].AsInt(),
              Status = reader["Status"].AsBool(),
              ShowDefault = reader["ShowDefault"].AsBool(),
              HasImage = reader["HasImage"].AsBool(),
              MultiRow = reader["MultiRow"].AsBool()
          };

        /// <summary>
        /// Creates query parameters list from UserGroup object
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>
        /// Name value parameter list.
        /// </returns>
        private static object[] TakeUpdate(Permission item)
        {
            return new object[]
                       {
                           "@Id", item.Id,
                           "@Title", item.Title,
                           "@Code", item.Code,
                           "@Description", item.Description,
                           "@Order", item.Order,
                           "@Status", item.Status,
                           "@ShowDefault", item.ShowDefault,
                           "@HasImage", item.HasImage,
                           "@MultiRow",item.MultiRow                           
                       };
        }

        /// <summary>
        /// Takes the insert.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        private static object[] TakeInsert(Permission item)
        {
            return new object[]
                       {                           
                           "@Title", item.Title,
                           "@Code", item.Code,
                           "@Description", item.Description,
                           "@Order", item.Order,
                           "@Status", item.Status,
                           "@ShowDefault", item.ShowDefault,
                           "@HasImage", item.HasImage,
                           "@MultiRow",item.MultiRow
                       };
        }
    }
}
